import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:icooker/model/reommend_data.dart';
import 'package:kasax/kasax.dart';
import 'package:video_player/video_player.dart';

/// 视频播放: Chewie
class JCChewieVideoPlayer extends StatefulWidget {
  final String videoUr;

  JCChewieVideoPlayer({Key? key, this.videoUr = ""}) : super(key: key);

  @override
  _JCChewieVideoPlayerState createState() => _JCChewieVideoPlayerState();
}

class _JCChewieVideoPlayerState extends State<JCChewieVideoPlayer> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.network(widget.videoUr);
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          child: FutureBuilder(
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasData) {
              if (snapshot.data == true) {
                return widget.videoUr == null || widget.videoUr.isEmpty
                    ? Container(
                        child: buildLoadingWidget(),
                      )
                    : Chewie(
                        controller: _chewieController,
                      );
              }
            }
          }
          return buildLoadingWidget();
        },
        future: initPlayer(),
      )),
      backgroundColor: Colors.black45,
    );
  }

  Future<bool> initPlayer() async {
    await _videoPlayerController.initialize();
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      autoPlay: true,
      looping: true,
      // 拖动条样式颜色
      materialProgressColors: new ChewieProgressColors(
        playedColor: Colors.red,
        handleColor: Colors.blue,
        backgroundColor: Colors.grey,
        bufferedColor: Colors.lightGreen,
      ),
    );
    return true;
  }
}
